package com.cfinnovate.example;

import java.util.List;

import com.naryx.tagfusion.cfm.engine.cfData;
import com.naryx.tagfusion.cfm.engine.cfQueryResultData;
import com.naryx.tagfusion.cfm.engine.cfSession;
import com.naryx.tagfusion.cfm.engine.cfStructData;
import com.naryx.tagfusion.cfm.engine.cfmRunTimeException;
import com.naryx.tagfusion.expression.function.functionBase;

public class QueryGetRow extends functionBase {

	private static final long serialVersionUID = -437489779240003570L;

public QueryGetRow() { min = max = 2; }

public cfData execute( cfSession _session, List<cfData> parameters ) throws cfmRunTimeException {
      cfData query = parameters.get(1);
int rowNo = parameters.get(0).getInt();

      if ( query.getDataType() == cfData.CFQUERYRESULTDATA ){
         cfQueryResultData queryData = (cfQueryResultData)query;
         if ( rowNo > queryData.getNoRows() || rowNo < 1 ){
                        throwException( _session, "the row " + rowNo + " does not exist" );

         }

         cfStructData struct = new cfStructData();
    String[] columnList = queryData.getColumnNames();
   
    for ( int i = 0; i < columnList.length; i ++ ) {
       struct.put(columnList[i], queryData.getCell(rowNo, i+1));
    }
    return struct;
      }else
         throwException( _session, "the parameter is not a Query" );
         return null;
   }
}